package com.webull.information.center.carwler.common.util.jsoup.prase_en;

import java.io.IOException;
import java.text.ParseException;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.util.jsoup.HtmlBodyPrase;
import com.webull.information.center.carwler.common.util.jsoup.JsoupPraseUtil;
import com.webull.information.center.common.constants.Constants;

/**
 * 路透新闻解析器
 * 
 * @author shimingjun
 * @date 2016年8月19日 下午8:30:39
 * @version 1.0
 * @since JDK 1.8
 */
public class Reuters_HtmlPrase implements HtmlBodyPrase {
	protected final Logger logger = LogManager.getLogger(getClass());

	/**
	 * for example :http://www.reuters.com/article/idUSL4N1981V1
	 */
	@Override
	public void praseNewsInfo(org.jsoup.nodes.Document doc, NewsInformation info) {
		try {
			Optional.ofNullable(doc.getElementById("content"))
					.map(contex -> contex.select("h1.article-headline").first())
					.map(h1 -> StringUtils.stripToNull(h1.html())).ifPresent(title -> info.setTitle(title));

			if (StringUtils.isBlank(info.getSourceName())) {
				info.setSourceName("reuters.com");
			}
			Element body = doc.getElementById("article-text");
			if (body == null)
				return;

			Element article_info = Optional.ofNullable(doc.getElementById("content"))
					.map(contex -> contex.select("h1.article-headline").first()).map(h1 -> h1.previousElementSibling())
					.orElse(null);

			// Optional.ofNullable(article_info).map(sib ->
			// sib.select("span.article-section").first()).map(source1 ->
			// source1.getElementsByTag("A").first())
			// .map(a0 -> StringUtils.stripToNull(a0.html())).ifPresent(s_name
			// -> info.setSourceName(s_name));

			Optional.ofNullable(article_info).map(sib -> sib.select("span.timestamp").first())
					.map(time0 -> StringUtils.stripToNull(time0.html())).ifPresent(str -> {
						try {
							String edt = StringUtils.stripToNull(StringUtils.substringAfterLast(str, " "));
							String timz = StringUtils.stripToEmpty(StringUtils.substringBeforeLast(str, " "));
							timz = StringUtils.replace(timz, " | ", " ");
							info.setPushTime(timz + " " + edt);// Wed Jun 15,
																// 2016 11:39pm
							if (StringUtils.equalsIgnoreCase("bjt", edt)) {// 中国时间
								// 2014年 9月 30日 星期二 11:14 BJT
								Optional.ofNullable(UtilDate.parse(timz, Locale.CHINESE, TimeZone.getTimeZone(edt),
										"yyyy年MM月dd日 EEEE hh:mm")).ifPresent(d -> info.setNewsTime(d));
								if (StringUtils.isBlank(info.getLanguage())) {
									info.setLanguage(Constants.lang_zh);
								}
							} else {
								// Mon Mar 17, 2014 9:46pm EDT
								// Wed Sep 14, 2016 | 6:50am

								Optional.ofNullable(UtilDate.parse(timz, Locale.US, TimeZone.getTimeZone(edt),
										"E MMM dd, yyyy hh:mma")).ifPresent(d2 -> info.setNewsTime(d2));
								if (StringUtils.isBlank(info.getLanguage())) {
									info.setLanguage(Constants.lang_en);
								}
							}
						} catch (Exception e) {
						}

					});
			/**
			 * 设置新闻标题
			 */

			// 设置soure和pushTime

			// 新闻正文

			Optional.ofNullable(body.getElementsByTag("p")).ifPresent(ps0 -> {
				for (int i = ps0.size() - 1; i >= 0; i--) {
					Element p = ps0.get(i);
					JsoupPraseUtil.trimParagraph(p);
					if (!p.hasText()) {
						ps0.remove(i);
					}
					/**
					 * 删除源网页要隐藏的 地区块 <span class="articleLocation">HONG
					 * KONG</span>
					 */
					Optional.ofNullable(p.getElementsByClass("articleLocation").first()).ifPresent(sp -> sp.remove());
				}
				info.setContent(StringUtils.stripToNull(ps0.outerHtml()));
			});

		} catch (

		Exception e) {
			logger.warn(e);
		}
	}

	public static void main(String[] args) throws ParseException, IOException {
		String url2 = "http://www.reuters.com/article/idUSL4N1981V1";
		url2 = "http://in.reuters.com/article/idINL4N1A51ZO";
		url2 = "http://cn.reuters.com/article/baoshan-steel-idCNKCS0XP06L";

		url2 = "http://www.reuters.com/article/china-grand-automotive-services-co-ltd-b-idUSL3N18K1SK";

		url2 = "http://cn.reuters.com/article/st-idCNKCS0HP06R20140930";
		// url2 =
		// "http://www.reuters.com/article/us-gezhouba-argentina-idUSBRE9A00KH20131101";
		url2 = "http://www.reuters.com/article/idUSFit970721";
		url2 = "http://www.reuters.com/article/idUSL4N19R1JM?type=companyNews";
		// url2 =
		// "http://www.reuters.com/article/idUSL3N1BQ38V?type=companyNews";
		Connection connection = Jsoup.connect(url2).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));

		connection.proxy("127.0.0.1", 1080);
		org.jsoup.nodes.Document doc = connection.timeout(10000).get();
		NewsInformation info = new NewsInformation();
		new Reuters_HtmlPrase().praseNewsInfo(doc, info);
		System.out.println(info);

		// Mon Mar 17, 2014 9:46pm EDT
		// Wed Jan 15 00:00:00 CST 2014
		// Date d = UtilDate.parse("Mon Mar 17, 2014 9:46pm", Locale.US,
		// TimeZone.getTimeZone("EDT"), "E MMM dd, yyyy hh:mma");
		// System.out.println(d);

	}
}
